package com.zuoshenge.main.init;

import com.zuoshenge.init.AbstractCache;
import com.zuoshenge.main.entity.Academy;
import com.zuoshenge.main.entity.Course;
import com.zuoshenge.main.service.CourseService;
import com.zuoshenge.redis.util.RedisUtil;
import com.zuoshenge.util.CaffeineCacheUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class InitCourseCache extends AbstractCache {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CaffeineCacheUtil caffeineCacheUtil;
    @Resource
    private CourseService courseService;
    @Value("${init.cache.course-cache-key}")
    private String COURSE_CACHE_KEY;
    @Value("${redis.keys.course-quota-key}")
    private String COURSE_QUOTA_KEY;

    @Override
    public void initCache() {
        List<Course> courseList = courseService.queryAll();
        courseList.forEach(course -> {
            caffeineCacheUtil.set(COURSE_CACHE_KEY + course.getCid(), course);
            redisUtil.set(COURSE_QUOTA_KEY + course.getCid(), course.getCurrentQuota());
        });
        System.out.println("course缓存预热成功");
    }

    @Override
    public <T> T getCache() {
        return null;
    }

    @Override
    public void clearCache() {

    }
}
